package com.example.myfirst.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.myfirst.dao.ShopCartDao;
import com.example.myfirst.entity.Goods;
import com.example.myfirst.entity.ShopCart;
import com.example.myfirst.service.GoodsService;
import com.example.myfirst.service.ShopCartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartDao shopCartDao;
    @Autowired
    private GoodsService goodsService;

    @Override
    public ShopCart findGoodsByUserIdAndGoodsId(Integer user_id, Integer goods_id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", user_id);
        qw.eq("goods_id", goods_id);
        return shopCartDao.selectOne(qw);
    }

    @ApiOperation("添加购物车")
    @Override
    public Boolean addShopCart(Integer user_id, Integer goods_id) {
        ShopCart shopCart = new ShopCart();
        // 当用户点击 "加入购物车" 时 数量默认为 一个
        shopCart.setUserId(user_id);
        shopCart.setGoodsId(goods_id);
        shopCart.setNum(1);

        int i = shopCartDao.insert(shopCart);
        return i > 0;
    }

    @ApiOperation("根据用户id查询购物车信息")
    @Override
    public List<ShopCart> queryShopCartByUserId(Integer userId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        List<ShopCart> list = shopCartDao.selectList(qw);
        for (ShopCart shopcart : list) {
            Goods goods = goodsService.queryGoodsById(shopcart.getGoodsId());
            shopcart.setGoods(goods);
        }
        return list;
    }

    @ApiOperation("根据用户id和商品id查找商品,查找到了再对数量进行加一操作")
    @Override
    public Boolean updateShopCart(Integer userId, Integer goodsId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("goods_id", goodsId);

        ShopCart shopCart = findGoodsByUserIdAndGoodsId(userId, goodsId);
        shopCart.setNum(shopCart.getNum() + 1);
        int i = shopCartDao.update(shopCart, qw);
        return i > 0;
    }

    @ApiOperation("根据用户id和商品id查找商品,然后对数量进行赋值操作")
    @Override
    public Boolean updateShopCart(Integer userId, Integer goodsId, Integer num) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("goods_id", goodsId);
        ShopCart shopCart = findGoodsByUserIdAndGoodsId(userId, goodsId);
        shopCart.setNum(num);
        int i = shopCartDao.update(shopCart, qw);
        return i > 0;
    }

    @ApiOperation("根据用户id和商品id删除商品")
    @Override
    public Boolean delShopCart(Integer userId, Integer goodsId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.eq("goods_id", goodsId);

        int i = shopCartDao.delete(qw);
        return i > 0;
    }

    @ApiOperation("根据用户id和商品id集合查找商品")
    @Override
    public List<ShopCart> queryShopCartByUidAndIds(Integer userId, String ids) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.in("goods_id", ids.split(","));
        List<ShopCart> list = shopCartDao.selectList(qw);
        for (ShopCart shopcart : list) {
            Goods goods = goodsService.queryGoodsById(shopcart.getGoodsId());
            shopcart.setGoods(goods);
        }
        return list;
    }

    @Override
    public Boolean delShopCartBatch(Integer userId, String ids) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        qw.in("goods_id", ids.split(","));

        int i = shopCartDao.delete(qw);
        return i > 0;
    }
}
